package cn.declare.controller;

import cn.declare.bean.Appealreporting;
import cn.declare.bean.Appealtype;
import cn.declare.bean.Complete;
import cn.declare.dto.AppealreportingDto;
import cn.declare.service.AppealreportingService;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Author
 * 潘红阳
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/appeal")
public class AppealreportingController {

    @Autowired
    private AppealreportingService appealreportingService;

    @Autowired
    private Complete complete;

    /**
     * @param completestatus 处理状态
     * @param userid 用户id
     * @param tradeId 行业id
     * @param currpage 当前页数
     * @return
     * 分页查询不同处理状态下的所有诉求
     * 三角色共用
     */
    @RequestMapping("/selectappealAllbyStatus/{completestatus}/{rejectid}/{userid}/{tradeId}/{currpage}")
    public List<AppealreportingDto> selectappealAllbyStatus(@PathVariable("completestatus") int completestatus,
                                                            @PathVariable("rejectid") int rejectid,
                                                         @PathVariable("userid") int userid,
                                                         @PathVariable("tradeId")int tradeId,
                                                         @PathVariable("currpage")int currpage,
                                                            HttpServletRequest request){
        String token = request.getHeader("token");
//        在控制台打印token
        System.out.println("----============----");
        System.out.println(token);

        List<AppealreportingDto> appealreportings = appealreportingService.selectappealAllbyStatus(completestatus, rejectid,userid, tradeId, currpage, 5);
        return appealreportings;
    }

    /**
     * @param appealreportingid
     * @return
     * 根据诉求id查询对因的数据
     */
    @RequestMapping("/selectappealByid/{appealreportingid}")
    public AppealreportingDto selectappealByid(@PathVariable("appealreportingid") int appealreportingid){
        AppealreportingDto appealreportingDto = appealreportingService.selectappealByid(appealreportingid);
        return appealreportingDto;
    }

    /**
     * @param userid
     * @return
     * 通过用户id查询草稿箱
     *
     */
    @RequestMapping("/selectapplebysavestatus/{userid}")
    public AppealreportingDto selectapplebysavestatus(@PathVariable("userid") int userid){
        AppealreportingDto selectapplebysavestatus = appealreportingService.selectapplebysavestatus(userid);
        return selectapplebysavestatus;
    }

    /**
     * @param appealreporting
     * @return
     * 通过用户id修改草稿箱
     * 需要先判断之前是否存过草稿，如果没有，调用添加方法savestatus=1
     */
    @RequestMapping("/applebysavestatusUpdate")
    public String applebysavestatusUpdate(@RequestBody Appealreporting appealreporting){
        AppealreportingDto selectapplebysavestatus = appealreportingService.selectapplebysavestatus((int) appealreporting.getUserId());
        if (selectapplebysavestatus!=null){
            int i = appealreportingService.applebysavestatusUpdate(appealreporting);
            if (i>0){
                return "success";
            }else {
                return "defeated";
            }
        }else {
            appealreporting.setSaveStatus(1);
            appealreporting.setAppealDate(new Date());
            appealreporting.setCompletestatus(1);
            appealreporting.setRejectid(1);
            int i = appealreportingService.appealAdd(appealreporting);
            if (i>0){
                return "success";
            }else {
                return "defeated";
            }
        }
    }


    /**
     * @return
     * 查询所有诉求类型
     */
    @RequestMapping("/selectAllappealtype")
    public List<Appealtype> selectAllappealtype(){
        List<Appealtype> appealtypes = appealreportingService.selectAllappealtype();
        return appealtypes;
    }

    /**
     * @param completestatus
     * @param userid
     * @param tradeId
     * @return
     * 查询总页数
     */
    @RequestMapping("/pagecount/{completestatus}/{userid}/{tradeId}")
    public int pagecount(@PathVariable("completestatus") int completestatus,@PathVariable("userid")int userid,@PathVariable("tradeId")int tradeId){
        int pagecount = appealreportingService.pagecount(completestatus, userid, tradeId);
        return pagecount;
    }


    /**
     * @param appealreporting
     * @return
     * 诉求添加
     */
    @RequestMapping("/appealAdd")
    public String appealAdd(@RequestBody Appealreporting appealreporting){
        appealreporting.setSaveStatus(2);
        appealreporting.setAppealDate(new Date());
        appealreporting.setRejectid(1);
        appealreporting.setCompletestatus(1);
        appealreporting.setTradeId(1);
        int i = appealreportingService.appealAdd(appealreporting);
        if (i>0){
            return "success";
        }else {
            return "defeated";
        }
    }


    /**
     * @param appealreporting
     * @return
     * 修改诉求内容
     */
    @RequestMapping("/appealUpdate")
    public String appealUpdate(@RequestBody Appealreporting appealreporting){

            appealreporting.setRejectid(1);
            int i = appealreportingService.appealUpdate(appealreporting);
            if (i>0){
                return "success";
            }else {
                return "defeated";
            }
    }

    /**
     * @param appealreportingid
     * @return 诉求状态修改
     * 诉求被驳回时修改状态为1
     * 诉求被通过时修改状态为2
     * 诉求完结时修改状态为3
     */
    @RequestMapping("/appealUpdatebyid/{completestatus}/{appealreportingid}")
    public String appealUpdatebyid(@PathVariable("completestatus") int completestatus,@PathVariable("appealreportingid") int appealreportingid){
        int i = appealreportingService.appealUpdatebyid(completestatus, appealreportingid);
        if (i>0){
            return "success";
        }else {
            return "defeated";
        }
    }

    /**
     * @return
     * 将诉求状态转变为被退回
     * 并书写退回原因
     */
    @RequestMapping("/appealrejectid/{rejectid}/{rejectcontent}/{appealreportingid}")
    public String appealrejectid(@PathVariable("rejectid") int rejectid,
                                 @PathVariable("rejectcontent") String rejectcontent,
                                 @PathVariable("appealreportingid") int appealreportingid){
        int i = appealreportingService.appealrejectid(rejectid, rejectcontent, appealreportingid);
        if (i>0){
            return "success";
        }else {
            return "defeated";
        }
    }

    /**
     * @param
     * @return 添加诉求回复
     * 企业端，管理端
     */
    @RequestMapping("/completeAdd/{appealreportingid}/{feedbackcontent}/{feedbackdepar}")
    public String completeAdd(@PathVariable("appealreportingid")int appealreportingid,
                              @PathVariable("feedbackcontent")String feedbackcontent,
                              @PathVariable("feedbackdepar")int feedbackdeparid){
        AppealreportingDto appealreportingDto = appealreportingService.selectappealByid(appealreportingid);
        if (appealreportingDto.getCompletestatus()==3){
            return "defail";
        }

        if (feedbackdeparid==1){
            complete.setFeedbackdepar("企业");
        }else {
            complete.setFeedbackdepar("政府");
        }
        complete.setAppealreportingid(appealreportingid);
        complete.setFeedbackdate(new Date());
        complete.setFeedbackcontent(feedbackcontent);
        int i = appealreportingService.completeAdd(complete);
        if (i>0){
            return "success";
        }else {
            return "defeated";
        }
    }

    /**
     * @param appealreportingid
     * @return 根据诉求id查询所有诉求回复内容
     */
    @RequestMapping("/selectCompleteAllbyid/{appealreportingid}")
    public List<Complete> selectCompleteAllbyid(@PathVariable("appealreportingid") int appealreportingid){
        List<Complete> completes = appealreportingService.selectCompleteAllbyid(appealreportingid);
        return completes;
    }

}
